package com.fuyc.project.tools.utils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
/**
 * 解析注解相关参数
 * */
public class ParseAnnotationUtils {
	/***
	* 通过类全名获取对象
	* @throws ClassNotFoundException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	* */
	public static Class<?> getObjectByFullClassName(String fullClassName) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		Class<?> clazz = Class.forName(fullClassName);
		return clazz;
	}
	
	/**
	 * 获取某个标记注解的方法,获取到一个就返回，所以一个对象中只能有一个该注解
	 * @throws Exception 
	 * */
	public static Method getMethodByAnnotion(Class<?> clazz,@SuppressWarnings("rawtypes") Class annotionCalss) throws Exception {
		if(clazz==null) {
			throw new Exception("传入的对象为空，获取不到任何可用方法！");
		}
		Method[] methods = clazz.getMethods();
		if(methods==null||methods.length==0) {
			throw new Exception("该对象中不存在任何方法！");
		}
		for(Method m:methods) {
			@SuppressWarnings("unchecked")
			Annotation annotation = m.getAnnotation(annotionCalss);
			if(annotation!=null) {
				return m;
			}
		}
		throw new Exception("该对象中不存在任何含标记有"+annotionCalss.getName()+"的方法");
	}
	/**
	 * 执行方法
	 * @throws Exception 
	 * */
	public static Object executeAnnotionMethod(Object o,Method m,Object param) throws Exception {
		if(m==null) {
			throw new Exception("不存在该方法");
		}
		Object result = m.invoke(o, param);
		return result;
	}
	
	
	
}
